#include "mysql_chat_session.hpp"
#include "mysql_apply.hpp"
#include "mysql_relation.hpp"
#include <gflags/gflags.h>

DEFINE_bool(run_mode, false, "程序的运行模式，false： 调试 true： 发布");
DEFINE_string(log_file, "", "发布模式下，向文件中输出，如果是调试模式下，模式是stdout");
DEFINE_int32(log_level, 0, "发布模式下，用于指定的日志输出等级");

DEFINE_string(host, "127.0.0.1", "这是mysql的地址");
DEFINE_int32(port, 0, "这是mysql的端口");
DEFINE_string(db, "lch_im", "数据库默认的库");
DEFINE_string(user, "root", "这是用户名");
DEFINE_string(passward, "123456", "这是密码");
DEFINE_string(cset, "utf8", "这是mysql客户端字符集");
DEFINE_int32(max_pool, 3, "这是mysql连接池最大连接数量");

void r_insert_test(RelationTable& tb)
{
    tb.insert("用户ID1","用户ID2");
    tb.insert("用户ID1","用户ID3");
}

void r_select_test(RelationTable& tb)
{
    auto res = tb.friends("ff46-b0af4a82-0000");
    for(auto uid : res) {
        std::cout << uid << std::endl;
    }
}

void r_remove_test(RelationTable &tb) {
    tb.remove("用户ID2", "用户ID1");
}

void r_exists_test(RelationTable &tb) {
    std::cout << tb.exists("用户ID2", "用户ID1") << std::endl;
    std::cout << tb.exists("用户ID3", "用户ID1") << std::endl;
}

void a_insert_test(FriendApplyTable& tb)
{
    FriendApply fa1("uuid1", "用户ID1", "用户ID2");
    tb.insert(fa1);

    FriendApply fa2("uuid2", "用户ID3", "用户ID2");
    tb.insert(fa2);
}

void a_remove_test(FriendApplyTable &tb) {
    tb.remove("用户ID2", "用户ID3");
}

void a_select_test(FriendApplyTable &tb) {
    auto res = tb.applyUsers("用户ID2");
    for (auto &uid:res) {
        std::cout << uid << std::endl;
    }
}

void a_exists_test(FriendApplyTable &tb) {
    std::cout << tb.exists("4f92-e45fe01f-0000", "ff46-b0af4a82-0000") << std::endl;
}

void c_insert_test(ChatSessionTable &tb) {
    ChatSession cs1("会话ID1", "会话名称1", ChatSessionType::SINGLE);
    tb.insert(cs1);
    ChatSession cs2("会话ID2", "会话名称2", ChatSessionType::GROUP);
    tb.insert(cs2);
}


void c_select_test(ChatSessionTable &tb) {
    auto res = tb.select("会话ID1");
    std::cout << res->chat_session_id() << std::endl;
    std::cout << res->chat_session_name() << std::endl;
    std::cout << (int)res->chat_session_type() << std::endl;
}

void c_single_test(ChatSessionTable &tb) {
    auto res = tb.singleChatSession("a40a-8c284cba-0000");
    for (auto &info : res) {
        std::cout << info.chat_session_id << std::endl;
        std::cout << info.friend_id << std::endl;
    }
}
void c_group_test(ChatSessionTable &tb) {
    auto res = tb.groupChatSession("用户ID1");
    for (auto &info : res) {
        std::cout << info.chat_session_id << std::endl;
        std::cout << info.chat_session_name << std::endl;
    }
}
void c_remove_test(ChatSessionTable &tb) {
    tb.remove("会话ID3");
}

void c_remove_test2(ChatSessionTable &tb) {
    tb.remove("a40a-8c284cba-0000", "afb0-40d41359-0000");
}
 
int main(int argc, char *argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    auto db = ODBFactory::create(FLAGS_user, FLAGS_passward, FLAGS_db, FLAGS_host, FLAGS_cset, 0, 1);
    RelationTable rtb(db);
    ChatSessionTable cstb(db);
    FriendApplyTable ftb(db);

    // r_insert_test(rtb);
     r_select_test(rtb);
    // r_remove_test(rtb);
    // r_exists_test(rtb);

    // a_insert_test(ftb);
    // a_remove_test(ftb);
    // a_select_test(ftb);
//     a_exists_test(ftb);

    // c_insert_test(cstb);
    // c_select_test(cstb);
    // c_single_test(cstb);
    // c_group_test(cstb);
//    c_remove_test2(cstb);

    return 0;
}
